# 创建空字典用来统计IP出现的次数

ip_count = {}
url_count = {}
with open('access.log-20191119') as f:
    lines = f.readlines()
for line in lines:
    # 通过spilt取得ip和url
    ip = line.strip().split()[0]
    url = line.strip().split()[6]
    # 如果在字典中那么值+1否则在字段中添加ip并赋值为1
    if ip in ip_count:
        ip_count[ip] += 1
    else:
        ip_count[ip] = 1
    if url in url_count:
        url_count[url] += 1
    else:
        url_count[url] = 1
 
# 最后使用items方法将字段转成元组的形式，进行排序，取出前十位
print("IP top 10")
for ip, count in  sorted(ip_count.items(), key=lambda x:x[1], reverse=True)[:10]:
    print(f'{ip} -- {count}') 
print("url top 10")
for url, count in  sorted(url_count.items(), key=lambda x:x[1], reverse=True)[:10]:
    print(f'{url} -- {count}')
# 图呢？记得要用html来展示统计信息的